package org.cebu.tourism.maps.data;

/*
 * Wrapper class for Map JSNI 
 * Map methods are declared here.
 */

import org.cebu.tourism.maps.overlay.MapEntry;
import org.cebu.tourism.maps.overlay.RouteEntry;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.geom.Size;
import com.google.gwt.maps.client.overlay.Icon;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;

public class MapDataWrapper{
	
	private final String ICON_PATH = "images/marker.png";
	private final int ICON_WIDTH = 40;
	private final int ICON_HEIGHT = 40;
	private final int INFO_OFFSET_X = 15;
	private final int INFO_OFFSET_Y = 1;
	private final int SHADOW_OFFSET_XY = 20;
    private static final MapList mapList = new MapList();
    private static final RouteList routeList = new RouteList();
	/*
	 * JSNI subclasses should be protected 
	 */
	protected MapDataWrapper(){}
    /*
     * @author: jdelapena 04/09/12 
     * @function Name: getMarker
     * @description: get marker for location
     * @return: Marker for the location
     */
	public final Marker getMarker(String location)
	{
		LatLng coords = search(location);
		MarkerOptions options = getMarkerOptions();
        final Marker marker = new Marker(coords, options);
		return marker;
	}
	/*
     * @author: jdelapena 04/09/12 
     * @function Name: setMarkerOptions
     * @description: set shadow, icon and anchor point
     *               options for marker
     * @return MarkerOptions
     */
	private final MarkerOptions getMarkerOptions()
	{
		Icon icon = Icon.newInstance(ICON_PATH);
		icon.setIconSize(Size.newInstance(ICON_WIDTH, ICON_HEIGHT));
		icon.setShadowSize(Size.newInstance(SHADOW_OFFSET_XY, SHADOW_OFFSET_XY));
		icon.setIconAnchor(Point.newInstance(ICON_WIDTH/2, ICON_HEIGHT));
		icon.setInfoWindowAnchor(Point.newInstance(INFO_OFFSET_X, INFO_OFFSET_Y));

		MarkerOptions options = MarkerOptions.newInstance();
		options.setIcon(icon);
		
		return options;
	}
    /*
     * @author: jdelapena 04/09/12 
     * @function Name: search
     * @description: search for location in JSNI Map List
     * @return: Coordinates (LatLng) for the location
     */
	public final LatLng search(String location)
	{
        JsArray<MapEntry> list = mapList.getMapList();
        
        int index = 0;
		while(index < list.length())
		{
			String result = list.get(index).location();			
			if(result.compareTo(location.toLowerCase()) == 0)
			{
				float lat = list.get(index).latitude();
				float longt = list.get(index).longitude();
				System.out.println("Location: " + result);
				return LatLng.newInstance(lat, longt);
			}
			index++;
		}	
		System.out.println("search: Location NOT FOUND");
		return null;
	}
	
	public final String getEncodeLevel(String route)
	{
        JsArray<RouteEntry> list = routeList.getRouteList();
        
        int index = 0;
		while(index < list.length())
		{
			String primary = list.get(index).route();
			String reverse = list.get(index).reverse();
			if(primary.compareTo(route.toLowerCase()) == 0 ||
			   reverse.compareTo(route.toLowerCase()) == 0)
			{
				return list.get(index).levels();
			}
			index++;
		}	
		System.out.println("getEncodeLevel: Route NOT FOUND");
		return null;
	}
	
	public final String getEncodeString(String route)
	{
        JsArray<RouteEntry> list = routeList.getRouteList();
        
        int index = 0;
		while(index < list.length())
		{
			String primary = list.get(index).route();
			String reverse = list.get(index).reverse();
			if(primary.compareTo(route) == 0 ||
			   reverse.compareTo(route) == 0)
			{
				return list.get(index).points();
			}
			index++;
		}	
		System.out.println("getEncodeString: Route NOT FOUND");
		return null;
	}
}
